<?php
function struktur_organisasi(){
//PASANG JAVASCRIPT
my_set_file_js( 
	array(
		'components/system/jquery/jquery.treeview/lib/jquery.cookie.js',
		'components/system/jquery/jquery.treeview/jquery.treeview.js',
		'components/system/jquery/jquery.treeview/demo/demo.js',
	) 
 );
my_set_file_css(
	array('components/system/jquery/jquery.treeview/jquery.treeview.css')
);
my_set_code_js('
$(document).ready(function(){

$("#browser").treeview({
		persist: "location",
		collapsed: true,
		unique: true
	});
	});

'); 
return '<ul id="browser" class="filetree">
'.display_children( ) .'
</ul>';
}



// $parent is the parent of the children we want to see
// $level is increased when we go deeper into the tree,
//        used to display a nice indented tree
function display_children( ) {
	$xo = $level+1;
   // retrieve all children of $parent
   $result = my_query( "SELECT * FROM jabatan a 
	   INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id 
	   WHERE a.nama_jabatan <> '-' AND b.active='Y'  AND a.parent_id ='0' 
	   ORDER BY urut" );
   // display each child
   $view ='';
   while ($row = my_fetch_array($result)) {
		
		$view .= '<li><span class="folder">'.  $row['nama_jabatan']  .'</span>';

		$result2 = my_query( "SELECT * FROM jabatan WHERE nama_jabatan <> '-' AND  parent_id='{$row['jabatan_id']}'  
			ORDER BY urut" );
		if(my_num_rows($result2) > 0){
		$view .= '<ul>';
		while($row2 = my_fetch_array($result2)){
			$selisih = has_skip_jabatan($row2['jabatan_id'],$row['jabatan_id']); 
			if($selisih)
				$view .= '<li><span class="folder">'. ( $selisih > 0 ? str_repeat('_', $selisih) :'').($row2['nama_jabatan'] <> '-' ? $row2['nama_jabatan'] : '__________')  .'</span>'; 
			else
				$view .= '<li><span class="folder">'.  ($row2['nama_jabatan'] <> '-' ? $row2['nama_jabatan'] : '__________')  .'</span>'; 
	
			$res3 =  my_query( "SELECT * FROM jabatan WHERE nama_jabatan <> '-' AND parent_id='{$row2['jabatan_id']}' ORDER BY level" );
			if(my_num_rows($res3) > 0){
			$view .= '<ul>';
			while($row3 = my_fetch_array($res3)){
				$selisih = has_skip_jabatan($row3['jabatan_id'],$row2['jabatan_id']); 
				if($selisih)
					$view .= '<li><span class="folder">'. ( $selisih > 0 ? str_repeat('_', $selisih) :'').($row3['nama_jabatan'] <> '-' ? $row3['nama_jabatan'] : '__________')  .'</span>'; 
				else
				$view .= '<li><span class="folder">'.   ($row3['nama_jabatan'] <> '-' ? $row3['nama_jabatan'] : '__________')   .'</span>';
				
				$result4 = my_query( "SELECT * FROM jabatan WHERE  nama_jabatan <> '-' AND  parent_id='{$row3['jabatan_id']}' 
					ORDER BY level" );
				if(my_num_rows($result4) > 0){
				$view .= '<ul>';
				while($row4 = my_fetch_array($result4)){
					$selisih = has_skip_jabatan($row4['jabatan_id'],$row3['jabatan_id']); 
					if($selisih)
						$view .= '<li><span class="folder">'. ( $selisih > 0 ? str_repeat('_', $selisih) :'').($row4['nama_jabatan'] <> '-' ? $row4['nama_jabatan'] : '__________')  .'</span>'; 
					else
					$view .= '<li><span class="folder">'.   ($row4['nama_jabatan'] <> '-' ? $row4['nama_jabatan'] : '__________')  .'</span>';
				
					$result5 = my_query( "SELECT * FROM jabatan WHERE  nama_jabatan <> '-' AND  parent_id='{$row4['jabatan_id']}' ORDER BY level" );
					if(my_num_rows($result5) > 0){
					$view .= '<ul>';
					while($row5 = my_fetch_array($result5)){
						$selisih = has_skip_jabatan($row5['jabatan_id'],$row4['jabatan_id']); 
						if($selisih)
							$view .= '<li><span class="folder">'. ( $selisih > 0 ? str_repeat('_', $selisih) :'').($row5['nama_jabatan'] <> '-' ? $row5['nama_jabatan'] : '__________')  .'</span>'; 
						else
							$view .= '<li><span class="folder">'.  ($row5['nama_jabatan'] <> '-' ? $row5['nama_jabatan'] : '__________')   .'</span>';
				
						
						$result6= my_query( "SELECT * FROM jabatan WHERE  nama_jabatan <> '-' AND  parent_id='{$row5['jabatan_id']}' ORDER BY level" );
						if(my_num_rows($result6) > 0){
						$view .= '<ul>';
						while($row6 = my_fetch_array($result6)){
							
							$selisih = has_skip_jabatan($row6['jabatan_id'],$row5['jabatan_id']); 
							if($selisih)
								$view .= '<li><span class="folder">'. ( $selisih > 0 ? str_repeat('_', $selisih) :'').($row6['nama_jabatan'] <> '-' ? $row6['nama_jabatan'] : '__________')  .'</span>'; 
							else
								$view .= '<li><span class="folder">'. ($row6['nama_jabatan'] <> '-' ? $row6['nama_jabatan'] : '__________')  .'</span>';
				
							$result7= my_query( "SELECT * FROM jabatan WHERE  nama_jabatan <> '-' AND  parent_id='{$row6['jabatan_id']}' ORDER BY level" );
							if(my_num_rows($result7) > 0){
							$view .= '<ul>';
							while($row7 = my_fetch_array($result7)){
									$view .= '<li><span class="folder">'.  ($row7['nama_jabatan'] <> '-' ? $row7['nama_jabatan'] : '__________') .'</span>';
				
									$result8= my_query( "SELECT * FROM jabatan WHERE  nama_jabatan <> '-' AND  parent_id='{$row7['jabatan_id']}' ORDER BY level" );
									if(my_num_rows($result8) > 0){
									$view .= '<ul>';
									while($row8 = my_fetch_array($result8)){
										$view .= '<li><span class="file">'.  ($row8['nama_jabatan'] <> '-' ? $row8['nama_jabatan'] : '__________')   .'</span>';
				
									}
									$view .= '</ul>';
									}
							
							}
							$view .= '</ul>';
							}
						}
						$view .= '</ul>';
						}
				
				
					}
					$view .= '</ul>';
					}
				}
				$view .= '</ul>';
				}
			}
			$view .= '</ul>';
			}
		}
		$view .= '</ul>';
		}
   } 
   return $view;
} 

function has_skip_jabatan($jabatan_id,$parent_jabatan_id){
	$a = my_get_data_by_id('jabatan','jabatan_id',$parent_jabatan_id);
	$b = my_get_data_by_id('jabatan','jabatan_id',$jabatan_id);
	  
	$selisih = (int)$b['level'] - (int)$a['level'];
	if($selisih - 1 <= 0)return false;
	return $selisih - 1;
}



